extends KinematicBody2D

export (int) var SPEED = 200
var velocity = Vector2()
var shooting = true
onready var timer = get_node("Timer")

func _ready():
	# Called when the node is added to the scene for the first time.
	# Initialization here
	timer.stop()

	pass


func get_input():
	velocity = Vector2()
	if Input.is_action_pressed('ui_right'):
		velocity.x += 1
	elif Input.is_action_pressed('ui_left'):
		velocity.x -= 1
	elif Input.is_action_pressed('ui_down'):
		velocity.y += 1
	elif Input.is_action_pressed('ui_up'):
		velocity.y -= 1

	
	
	velocity = velocity.normalized() * SPEED

func _process(delta):
	var speed_x = 0

	
	position.x = position.x + speed_x
	
	if Input.is_action_pressed("ui_accept") :
		if timer.is_stopped():

			var fireball = load("res://bullet.tscn").instance() 
			var spawner = get_node("Position2D")
			
			fireball.position = spawner.get_global_position()
			get_node("/root").add_child(fireball)
			restart_timer()
	pass

func _physics_process(delta):

	get_input()
	var collision_info = move_and_collide(velocity * delta)
	if collision_info:
		collision_info.get_collider().free()
		free()
#func _process(delta):
#	# Called every frame. Delta is time since last frame.
#	# Update game logic here.
#	pass
func restart_timer():
	timer.start()
	timer.set_wait_time(0.5)





func _on_Timer_timeout():
	timer.stop()



